import indexConfig from '@/config/index.config'

export default {
	toast(title) {
		uni.showToast({
			title: title || '操作成功',
			icon: 'none'
		})
	},
	confirm(content, confirmCallback, cancelCallback) {
		uni.showModal({
			title: '温馨提示',
			content: content,
			success: (res) => {
				if (res.confirm) {
					if (typeof confirmCallback == "function") {
						confirmCallback()
					}
				} else if (res.cancel) {
					if (typeof cancelCallback == "function") {
						cancelCallback()
					}
				}
			}
		})
	},
	checkMobile(mobile) {
		let _reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
		if (_reg.test(mobile)) {
			return true
		}

		return false
	},
	checkPageRoute(page) {
		let pages = getCurrentPages()
		let currentPage = null
		if (pages.length) {
			currentPage = pages[pages.length - 1]
		}
		if (currentPage.route == page) {
			return true
		}

		return false
	},
	goPage(url) {
		uni.navigateTo({
			url: url
		})
	},
	goRedirect(url) {
		uni.redirectTo({
			url: url
		})
	},
	binImage(url, width, height, none) {
		if (!url) {
			return none || ''
		}
		width = width || 300
		height = height || 300

		let _url = indexConfig.imgUrl + url
		if (width != 1 && height != 1) {
			_url = _url + '!' + width + 'x' + height
		}
		return _url
	},
	previewImage(files, current) {
		if (!files) {
			return
		}

		let urls = []
		if (Array.isArray(files)) {
			files.forEach(item => {
				urls.push(indexConfig.imgUrl + item)
			})
		} else {
			urls = [indexConfig.imgUrl + files]
		}

		uni.previewImage({
			current: current || 0,
			urls: urls,
			longPressActions: {
				itemList: ['发送给朋友', '保存图片'],
				success: (res) => {},
				fail: (err) => {}
			}
		})
	},
	validate(formValue, ruleField) {
		if (!ruleField.length) {
			return true
		}

		let length = ruleField.length
		for (let i = 0; i < length; i++) {
			if (!(formValue[ruleField[i]] || formValue[ruleField[i]] === 0)) {
				return false
				break
			}
		}
		return true
	},
	distance(distance) {
		if (!distance) {
			return '未知';
		} else if (distance <= 500) {
			return '<500m';
		} else if (distance > 500 && distance <= 1000) {
			return distance.toFixed(0);
		} else {
			distance = distance / 1000;
			distance = distance.toFixed(1);
			return distance + 'km';
		}
	},
	doCopy(data) {
		uni.setClipboardData({
			data: data
		})
		uni.showToast({
			icon: 'none',
			title: '复制成功',
		})
	},
	doCall(number) {
		uni.makePhoneCall({
			phoneNumber: '' + number,
			success: (e) => {},
			fail: (e) => {}
		})
	},
	orderStatus(value) {
		let status = {
			NO_PAY: '待支付',
			NO_ACCEPT: "待接单",
			NO_SERVICE: '待服务',
			NO_COMMENT: '待评价',
			FINISH: '已完成',
			CANCEL: '已取消',
			REFUNDING: '退款中',
			REFUND: '已退款',
			IN_SERVICE: '服务中',
		}

		if (value != 0 && !value) {
			return status
		}
		return status['' + value]
	},
	slotDatetime(datetime1, datetime2) {
		if (!datetime1) {
			return
		}
		let date1 = new Date(datetime1)

		let date2
		if (datetime2) {
			datetime2 = datetime2.replace(/-/g, "/")
			date2 = new Date(datetime2)
		} else {
			date2 = new Date()
		}

		let second = (date2.getTime() - date1.getTime()) / 1000
		return parseInt(second)
	},
}
